<?php
namespace app\index\controller;

use think\Controller;
use app\model\MqttLog;
use think\Db;

class MqttLogTest extends Controller
{
    /**
     * 测试日志记录功能
     */
    public function testLog()
    {
        try {
            // 模拟MQTT消息数据
            $testMessageData = [
                "api_version" => "1.0",
                "upload_data_type" => "normal",
                "cur_sim_id" => 2,
                "device_info" => [
                    "dev_imei1" => "863780074784151",
                    "dev_mac" => "1C:34:26:52:25:42"
                ],
                "sim_info" => [
                    [
                        "sim_id" => 2,
                        "sim_exist" => 1,
                        "sim_iccid" => "898604881924d1098999"
                    ]
                ],
                "test_timestamp" => date('Y-m-d H:i:s')
            ];
            
            $testImei = "863780074784151";
            $testIccid = "898604881924d1098999";

            // 测试日志记录
            $result = MqttLog::logMessage($testImei, $testMessageData, $testIccid);
            
            if ($result) {
                return json([
                    'code' => 1,
                    'msg' => '日志记录测试成功',
                    'data' => [
                        'imei' => $testImei,
                        'iccid' => $testIccid,
                        'message_size' => strlen(json_encode($testMessageData)),
                        'timestamp' => date('Y-m-d H:i:s')
                    ]
                ]);
            } else {
                return json([
                    'code' => 0,
                    'msg' => '日志记录测试失败'
                ]);
            }
            
        } catch (\Throwable $e) {
            return json([
                'code' => 0,
                'msg' => '测试异常: ' . $e->getMessage()
            ]);
        }
    }
    
    /**
     * 查看指定IMEI的日志
     */
    public function viewLogs()
    {
        $imei = $this->request->param('imei', '863780074784151');
        $limit = $this->request->param('limit', 10);
        
        try {
            $logs = MqttLog::getByImei($imei, $limit);
            
            return json([
                'code' => 1,
                'msg' => '查询成功',
                'data' => [
                    'imei' => $imei,
                    'total' => count($logs),
                    'logs' => $logs
                ]
            ]);
            
        } catch (\Throwable $e) {
            return json([
                'code' => 0,
                'msg' => '查询失败: ' . $e->getMessage()
            ]);
        }
    }
    
    /**
     * 查看日志表统计信息
     */
    public function logStats()
    {
        try {
            // 总记录数
            $totalCount = Db::connect('db_config2')->name('log')->count();
            
            // 今天的记录数
            $todayCount = Db::connect('db_config2')->name('log')
                ->whereTime('createtime', 'today')
                ->count();
            
            // 最近的10条记录
            $recentLogs = Db::connect('db_config2')->name('log')
                ->field('id,imei,iccid,createtime')
                ->order('createtime desc')
                ->limit(10)
                ->select();
            
            // 按IMEI统计
            $imeiStats = Db::connect('db_config2')->name('log')
                ->field('imei, count(*) as count')
                ->group('imei')
                ->order('count desc')
                ->limit(10)
                ->select();
            
            return json([
                'code' => 1,
                'msg' => '统计查询成功',
                'data' => [
                    'total_count' => $totalCount,
                    'today_count' => $todayCount,
                    'recent_logs' => $recentLogs,
                    'top_imei' => $imeiStats,
                    'timestamp' => date('Y-m-d H:i:s')
                ]
            ]);
            
        } catch (\Throwable $e) {
            return json([
                'code' => 0,
                'msg' => '统计查询失败: ' . $e->getMessage()
            ]);
        }
    }
    
    /**
     * 清理旧日志
     */
    public function cleanOldLogs()
    {
        $days = $this->request->param('days', 30);
        
        try {
            $deletedCount = MqttLog::cleanOldLogs($days);
            
            return json([
                'code' => 1,
                'msg' => '清理完成',
                'data' => [
                    'deleted_count' => $deletedCount,
                    'days' => $days,
                    'timestamp' => date('Y-m-d H:i:s')
                ]
            ]);
            
        } catch (\Throwable $e) {
            return json([
                'code' => 0,
                'msg' => '清理失败: ' . $e->getMessage()
            ]);
        }
    }
}
